package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.R;

import com.guoyansoft.model.dao.AddressDao;
import com.guoyansoft.model.dao.CustomerAndAddressDao;
import com.guoyansoft.model.pojo.Address;
import com.guoyansoft.model.pojo.Customer;
import com.guoyansoft.model.pojo.CustomerAndAddress;
import com.guoyansoft.model.service.AddressService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

/**
 * @author 常利军
 * 地址service实现类
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private CustomerAndAddressDao customerAndAddressDao;
    @Override
    public R queryById(Integer id) {
        Address address = addressDao.selectById(id);
        return R.suc(address);
    }

    @Override
    public R queryDistrict() {
        List<Address> addresses = addressDao.selectDistrict();
        return R.suc(addresses);
    }

    @Override
    public Address queryByAddress(Address address) {
        return addressDao.selectByAddress(address);
    }

    @Override
    public R queryAll() {
        List<Address> addresses = addressDao.selectAll();
        return R.suc(addresses);
    }




    @Override
    public R queryByPage(Map<String, Object> params) {
        Long total = addressDao.selectTotal(params);
        List<Address> addressList = addressDao.selectByPage(params);
        return R.suc( total,addressList);
    }

//    @Override
//    public R save(Address address,Integer customerId) {
//        Long row = addressDao.insert(address);
//        int row2 = customerAndAddressDao.insert(new CustomerAndAddress(new Customer(customerId), address));
//        return row>0&&row2>0?R.suc():R.fail();
//    }



    /*@Override
    public R queryTotal(Map<String, Object> params) {
        int total = addressDao.selectTotal(params);
        return R.suc(total);
    }*/

    @Override
    public R save(Address address,Integer customerId) {
        Long row = addressDao.insert(address);
        int row2 = customerAndAddressDao.insert(new CustomerAndAddress(new Customer(customerId), address));
        return row>0&&row2>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer[] id) {
        Address address=new Address();
        CustomerAndAddress customerAndAddress=new CustomerAndAddress();
        for (Integer i : id) {
            address.setId(i);
            customerAndAddress.setAddress(address);
        }
        Long row = customerAndAddressDao.deleteByAddressId(customerAndAddress);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R modify(Address address) {
        Long row = addressDao.update(address);
        return R.suc(row);
    }

    @Override
    public R queryByCustomerId(Integer customerId) {
        List<Address> addressList = addressDao.selectByCustomerId(customerId);
        return R.suc(addressList);
    }

}
